//
//  AdvanceDestoryLeaveView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/2.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "AdvanceDestoryLeaveView.h"

@implementation AdvanceDestoryLeaveView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = BackGroundColor;
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = CSColorFromRGB(0xfaf6e8);
        [scrollView addSubview:bgView];
        self.bgView = bgView;
        
        UIImageView *headImageView = [[UIImageView alloc] init];
        headImageView.image = [UIImage imageNamed:@"prompt_icon_careful"];
        [scrollView addSubview:headImageView];
        self.headImageView = headImageView;
        
        /**
         提示Label
         */
        UILabel *noticeLabel = [[UILabel alloc] init];
        noticeLabel.textColor = CSColorFromRGB(0xb3b3b3);
        noticeLabel.font = SmallFont;
        noticeLabel.text = @"从今天至";
        [scrollView addSubview:noticeLabel];
        self.noticeLabel = noticeLabel;
        
        UILabel *noticeLabel1 = [[UILabel alloc] init];
        noticeLabel1.textColor = CSColorFromRGB(0xb3b3b3);
        noticeLabel1.font = SmallFont;
        noticeLabel1.text = @"起提前销假，已销假";
        [scrollView addSubview:noticeLabel1];
        self.noticeLabel1 = noticeLabel1;
        
        UILabel *noticeLabel2 = [[UILabel alloc] init];
        noticeLabel2.textColor = CSColorFromRGB(0xb3b3b3);
        noticeLabel2.font = SmallFont;
        noticeLabel2.text = @"天";
        [scrollView addSubview:noticeLabel2];
        self.noticeLabel2 = noticeLabel2;
        
        
        /**
         原有效期
         */
        UILabel *originalLabel = [[UILabel alloc] init];
        originalLabel.font = SmallFont;
        originalLabel.text = @"2017-10-24";
        originalLabel.textColor = CSColorFromRGB(0x333333);
        [scrollView addSubview:originalLabel];
        self.originalLabel = originalLabel;
        
        /**
         天数
         */
        
        UILabel *dayCountLabel = [[UILabel alloc] init];
        dayCountLabel.font = SmallFont;
        dayCountLabel.text = @"24";
        [scrollView addSubview:dayCountLabel];
        self.dayCountLabel = dayCountLabel;
        
        
        /**
         白色
         */
        UIView *whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = CSWhiteColor;
        [scrollView addSubview:whiteView];
        self.whiteView = whiteView;
        
        /**
         销毁label
         */
        UILabel *destoryLabel = [[UILabel alloc] init];
        destoryLabel.font = MidFont;
        destoryLabel.text = @"销毁后有效期";
        destoryLabel.textColor = CSColorFromRGB(0x333333);
        [whiteView addSubview:destoryLabel];
        self.destoryLabel = destoryLabel;
        
        /**
         销毁时间Label
         */
        UILabel *destoryTimeLabel = [[UILabel alloc] init];
        destoryTimeLabel.font = TitleBoldFont;
        destoryTimeLabel.text = @"2017-10-30至2018-10-31";
        [whiteView addSubview:destoryTimeLabel];
        self.destoryTimeLabel = destoryTimeLabel;
        
        /**
         线条
         */
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:lineView];
        self.lineView = lineView;
        
        /**
         原有效期
         */
        UILabel *validTimeLabel = [[UILabel alloc] init];
        validTimeLabel.font = MidFont;
        validTimeLabel.text = @"原有效期：2017-10-01 至 2017-10-30";
        validTimeLabel.textColor = CSColorFromRGB(0xb3b3b3);
        [whiteView addSubview:validTimeLabel];
        self.validTimeLabel = validTimeLabel;
        
        /**
         白色
         */
        UIView *whiteView1 = [[UIView alloc] init];
        whiteView1.backgroundColor = CSWhiteColor;
        [scrollView addSubview:whiteView1];
        self.whiteView1 = whiteView1;

        /**
         是否退款
         */
        CommentSwitchView *refundSwitchView = [[CommentSwitchView alloc] init];
        [refundSwitchView.titleSwitch addTarget:self action:@selector(refundSwitchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
        refundSwitchView.titleLabel.text = @"是否退款";
        [whiteView1 addSubview:refundSwitchView];
        self.refundSwitchView = refundSwitchView;
        
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self);
    }];
    
    /**
     背景
     */
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.width.mas_equalTo(self.scrollView);
        
        make.height.mas_equalTo(40);
    }];
    
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.bgView);
        
        make.left.mas_equalTo(self.bgView).offset(15);
    }];
    
    [self.noticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.headImageView);
        
        make.left.mas_equalTo(self.headImageView.mas_right).offset(5);
    }];
    
    [self.originalLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.noticeLabel);
        
        make.left.mas_equalTo(self.noticeLabel.mas_right).offset(2);
    }];
    
    [self.noticeLabel1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.noticeLabel);
        
        make.left.mas_equalTo(self.originalLabel.mas_right).offset(2);
    }];
    
    /**
     天数
     */
    
    [self.dayCountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.noticeLabel);
        
        make.left.mas_equalTo(self.noticeLabel1.mas_right).offset(2);
    }];
    
    [self.noticeLabel2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.noticeLabel);
        
        make.left.mas_equalTo(self.dayCountLabel.mas_right);
    }];
    
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.width.mas_equalTo(self);
        
        make.top.mas_equalTo(self.bgView.mas_bottom);
        
    }];

    /**
     销毁label
     */
    [self.destoryLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.whiteView);

        make.top.mas_equalTo(self.whiteView).offset(40);
        
    }];
    
    /**
     销毁时间Label
     */
    [self.destoryTimeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self);

        make.top.mas_equalTo(self.destoryLabel.mas_bottom).offset(20);
    }];

    /**
     线条
     */
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self).offset(20);

        make.right.mas_equalTo(self).offset(-20);

        make.height.mas_equalTo(1);

        make.top.mas_equalTo(self.destoryTimeLabel.mas_bottom).offset(50);
    }];


    /**
     原有效期
     */
    [self.validTimeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self.lineView);

        make.top.mas_equalTo(self.lineView.mas_bottom).offset(10);

        make.bottom.mas_equalTo(self.whiteView).offset(-10);
    }];

    [self.whiteView1 mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(self.whiteView.mas_bottom).offset(10);

        make.left.width.mas_equalTo(self);
    }];
    
    [self.refundSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.left.width.mas_equalTo(self.whiteView1);

        if (!self.refundSwitchView.titleSwitch.isOn) {

            make.bottom.mas_equalTo(self.whiteView1);
        }
    }];

    if (self.refundSwitchView.titleSwitch.isOn) {

        /**
         支付方式
         */
        [self.payStyleView mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.top.mas_equalTo(self.refundSwitchView.mas_bottom);

            make.left.width.mas_equalTo(self);
        }];

        /**
         金额
         */
        [self.moneyInputView mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.top.mas_equalTo(self.payStyleView.mas_bottom);

            make.left.width.mas_equalTo(self);

            make.bottom.mas_equalTo(self.whiteView1);
        }];
        
        [self setNeedsLayout];
        [self layoutIfNeeded];
        
        self.scrollView.contentSize = CGSizeMake(self.width, self.moneyInputView.bottom + 10 + adaptNavagationBar);
    }
    else{
        
        [self setNeedsLayout];
        [self layoutIfNeeded];
        
        self.scrollView.contentSize = CGSizeMake(self.width, self.refundSwitchView.bottom + 10 + adaptNavagationBar);
    }
    
}

-(void)refundSwitchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        /**
         支付方式
         */
        CommonBtnView *payStyleView = [[CommonBtnView alloc] init];
        payStyleView.titleLabel.text = @"支付方式";
        payStyleView.choseLabel.text = @"请选择支付方式";
        payStyleView.choseLabel.textColor = PlaceHoldColor;
        [payStyleView.choseBtn addTarget:self action:@selector(payStyleBtnClick)];
        [self.whiteView1 addSubview:payStyleView];
        self.payStyleView = payStyleView;
        
        /**
         金额
         */
        CommonInputView *moneyInputView = [[CommonInputView alloc] init];
        moneyInputView.titleLabel.text = @"金额";
        moneyInputView.titleTF.placeholder = @"请输入金额";
        [self.whiteView1 addSubview:moneyInputView];
        self.moneyInputView = moneyInputView;
    }
    else{
        
        /**
         支付方式
         */
        [self.payStyleView removeFromSuperview];
        
        /**
         支付方式
         */
        [self.moneyInputView removeFromSuperview];
    }
    
    [self setConstraints];
}

#pragma mark PayStyleBtnClick
-(void)payStyleBtnClick{
    
    if ([self.choseBtnDelegate respondsToSelector:@selector(chosePayBtnClick)]) {
        
        [self.choseBtnDelegate chosePayBtnClick];
    }
}


@end
